'''###########################################################################################################################
### File: general_information_object.py
### Name: Patrick Delaney, Andrew Oberlin
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Final Project
### Description: Takes in the general information for teh main character
################ 
'''
from panda3d.core import Vec2, Point3, Vec3, Vec4
from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
from menu_button import MenuButton
from Tkinter import Tk
from tkFileDialog import askopenfilename
from panda3d.core import Filename
from shutil import copy
import os

class GeneralInformationObjectGUI:
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Starts the process of creating a main character
	'''
	def __init__(self, parent, objGen, object):
		self.parent = parent
		self.objGen = objGen
		self.object = object
		self.isPlatform = 0
		self.textureFile = self.object.getMainTexture()
		self.models = dict()
		self.labels = dict()
		self.__loadModels()
	
	'''
	### Name: __loadModels
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Loads the models for the GUI
	'''	
	def __loadModels(self):
		# Input for the name of the character
		self.models['name_input_dummy'] = self.parent.base.aspect2d.attachNewNode('name_input_dummy')
		self.models['name_input_dummy'].setPos(-1.25, 0, 0.5)
		
		self.labels['name_label_text'] = TextNode('name_label_text')
		self.labels['name_label_text'].setText("Object's Name")
		self.models['name_label'] = self.models['name_input_dummy'].attachNewNode(self.labels['name_label_text'])
		self.models['name_label'].setScale(Vec3(0.10,0.10,0.075))
		
		self.models['name_input'] = DirectEntry(initialText=self.object.getName(), cursorKeys=True, scale=Vec3(0.10, 0.075, 0.075))
		self.models['name_input'].setPos(Vec3(0.85, 0, 0))
		self.models['name_input'].reparentTo(self.models['name_input_dummy'])
		
		# Input for the texture file for the main charcter (this is the default texture file)		
		self.models['texture_input_dummy'] = self.parent.base.aspect2d.attachNewNode('texture_input_dummy')
		self.models['texture_input_dummy'].setPos(-1.25, 0, 0.3)
		
		self.labels['texture_label_text'] = TextNode('texture_label_text')
		self.labels['texture_label_text'].setText("Main Texture")
		self.models['texture_label'] = self.models['texture_input_dummy'].attachNewNode(self.labels['texture_label_text'])
		self.models['texture_label'].setScale(Vec3(0.10,0.10,0.075))
		
		self.models['preview_button'] = MenuButton(
			self.parent, 'load', 'models/gui/game_maker/dialog_button', 
			self.preview, Vec3(1.20, 0, 0.025), isMovie = False, text="Select Texture", 
			textPos=Vec3(-0.35, 0, -0.025), scale=Vec3(0.75,1,0.15)
		)
		
		self.models['preview_button'].reparentTo(self.models['texture_input_dummy'])
		
		self.models['preview'] = self.parent.loader.loadModelCopy("models/plane")
		self.models['preview'].reparentTo(self.parent.base.aspect2d)
		self.models['preview'].setPos(0.1, 0, -0.60)
		self.models['preview'].setScale(0.75)
		if (self.textureFile):
			texture = self.parent.loader.loadTexture(self.textureFile)
			self.models['preview'].setTransparency(1)
			self.models['preview'].setTexture(texture)
		
		self.models['next_step'] = MenuButton(
			self.parent, 'next_step', 'models/gui/game_maker/dialog_button', 
			self.objGen.nextStep, Vec3(0.8, 0, -0.80), isMovie = False, text="Next Step", 
			textPos=Vec3(-0.225, 0, -0.025), scale=Vec3(0.5,1,0.15)
		)
		
		# Declares this object to be a ground object or not (affects how texture is rendered)	
		self.models['ground_input_dummy'] = self.parent.base.aspect2d.attachNewNode('texture_input_dummy')
		self.models['ground_input_dummy'].setPos(-1.15, 0, 0.1)
		
		self.labels['ground_label_text'] = TextNode('ground_label_text')
		self.labels['ground_label_text'].setText("3D Ground Object?")
		self.models['ground_label'] = self.models['ground_input_dummy'].attachNewNode(self.labels['ground_label_text'])
		self.models['ground_label'].setScale(Vec3(0.10,0.10,0.075))
		self.models['ground_label'].setPos(0.1, 0, -0.025)
		self.models['ground_input'] = DirectCheckButton(text="", scale=.05, command=self.setIsPlatform, indicatorValue=self.object.getIsPlatform())
		self.models['ground_input'].reparentTo(self.models['ground_input_dummy'])
		
	'''
		Sets whether or not the current object is a ground object or not
	'''
	def setIsPlatform(self, bool):
		self.isPlatform = bool
		
	'''
		Gets whether or not the current object is a ground object or not
	'''
	def getIsPlatform(self):
		return self.isPlatform
	
	
	'''
	### Name: hide
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: hides this GUI
	'''
	def hide(self):
		for key in self.models.keys():
			self.models[key].hide()
			
	'''
	### Name: show
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: show this GUI
	'''
	def show(self):
		for key in self.models.keys():
			self.models[key].show()
	
		
	'''
	### Name: preview
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: finds the texture file for the main character
					 and loads it into a preview
	'''	
	def preview(self):
		try:
			root=Tk()
			self.textureFile = askopenfilename()
			#self.textureFile = Filename.fromOsSpecific(self.textureFile).getFullpath()
			root.destroy()
			
			basename = Filename(self.textureFile).getBasename()
			copy(self.textureFile, os.path.abspath("projects/" + self.objGen.project + "/objects/textures/" + basename))
			#os.system("cp " + self.textureFile + " projects/" + self.objGen.project + "/objects/textures/")
			self.textureFile = "projects/" + self.objGen.project + "/objects/textures/" + basename

			texture = self.parent.loader.loadTexture(self.textureFile)
			self.models['preview'].setTransparency(1)
			self.models['preview'].setTexture(texture)
		except IOError, e:
			print e.args[1]
			print "Problem loading texture!"
			
	'''
	### Name: getNameText
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: finds the text in the name input box
	'''	
	def getNameText(self):
		return self.models['name_input'].get(plain=True)
		
	'''
	### Name: getTextureFile
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: returns the texture file that has been chosen
	'''	
	def getTextureFile(self):
		return self.textureFile